package com.nononsenseapps.feeder.model

import androidx.room.ColumnInfo
import androidx.room.Ignore
import com.nononsenseapps.feeder.db.FEED
import com.nononsenseapps.feeder.db.SYSTEM_FEED_LIST
import com.nononsenseapps.feeder.db.entity.ATTENTION_NORMAL
import com.nononsenseapps.feeder.db.room.*
import com.nononsenseapps.feeder.util.sloppyLinkToStrictURLNoThrows
import java.net.URL


data class FeedUnreadCount @Ignore constructor(
    var id: Long = ID_UNSET,
    var title: String = "",
    var url: URL = sloppyLinkToStrictURLNoThrows(""),
    var tag: String = "",
    @ColumnInfo(name = FEED.COL_CUSTOM_TITLE) var customTitle: String = "",
    var notify: Boolean = false,
    @ColumnInfo(name = FEED.COL_IMAGE_URL) var imageUrl: URL? = null,
    @ColumnInfo(name = "unread_count") var unreadCount: Int = 0,
    @ColumnInfo(name = FEED.COL_ATTENTION_LEVEL) var attentionLevel: Int = ATTENTION_NORMAL,
    @ColumnInfo(name = FEED.COL_FEED_LAST_SYNC_SUCCESS) var syncSuccess: Boolean = true,
    @ColumnInfo(name = FEED.COL_FEED_SET_SYNC_ABLE) var syncAble: Boolean = true,
    @ColumnInfo(name = FEED.COL_RULE_ABLE) var ruleAble: Boolean = false
) {
    constructor() : this(id = ID_UNSET)

    val isReadLate: Boolean
        get() = id == READ_LATE_FEED_ID
    val displayTitle: String
        get() = (if (customTitle.isBlank()) title else customTitle)

    val isAllFeeds: Boolean
        get() = id == ID_ALL_FEEDS

    val isTag: Boolean
        get() = id < 1 && tag.isNotEmpty()

    val isFavorite:Boolean
        get()=id == FAVORITE_FEED_ID

    val isRule:Boolean
        get()=id == RULE_FEED_ID

    val isHot:Boolean
        get()=id == HOT_FEED_ID

    val isRecycle:Boolean
        get()=id == RECYCLE_FEED_ID

    val isStore:Boolean
        get()=id == STORE_FEED_ID

    val isSystemFeed:Boolean
        get()=id in SYSTEM_FEED_LIST

    val isMemo:Boolean
        get()=id == MEMO_FEED_ID

    val isBackup:Boolean
        get()=id == BACKUP_FEED_ID


    operator fun compareTo(other: FeedUnreadCount): Int {
        return when {
            // Top tag is always at the top (implies empty tags)
            isAllFeeds -> -1
            other.isAllFeeds -> 1
            //isFavorite->-1
            //other.isFavorite->1
            isRecycle -> 1
            other.isRecycle -> -1
            isSystemFeed->-1
            other.isSystemFeed->1
            // Feeds with no tags are always last
            isTag && !other.isTag && other.tag.isEmpty() -> -1
            !isTag && other.isTag && tag.isEmpty() -> 1
            !isTag && !other.isTag && tag.isNotEmpty() && other.tag.isEmpty() -> -1
            !isTag && !other.isTag && tag.isEmpty() && other.tag.isNotEmpty() -> 1
            // Feeds with identical tags compare by title
            tag == other.tag -> displayTitle.compareTo(other.displayTitle, ignoreCase = true)
            // In other cases it's just a matter of comparing tags
            else -> tag.compareTo(other.tag, ignoreCase = true)
        }
    }

    override fun equals(other: Any?): Boolean {
        return when (other) {
            null -> false
            is FeedUnreadCount -> {
                //val f = other as FeedWrapper?
                if (isTag && other.isTag) {
                    // Compare tags
                    tag == other.tag
                } else {
                    // Compare items
                    !isTag && !other.isTag && id == other.id
                }
            }
            else -> false
        }
    }

    override fun hashCode(): Int {
        return if (isTag) {
            // Tag
            tag.hashCode()
        } else {
            // Item
            id.hashCode()
        }
    }
}